var TChannel = require('tchannel');

// var server = new TChannel();
var client = new TChannel();

// var serverChan = server.makeSubChannel({
//     serviceName: 'server'
// });

// normal response
// serverChan.register('func1', function onReq(req, res, arg2, arg3) {
//     console.log('func1 responding', { arg2: arg2.toString(), arg3: arg3.toString() });
//     res.headers.as = 'raw';
//     res.sendOk('result', 'indeed it did');
// });
//
// // err response
// serverChan.register('func2', function onReq2(req, res) {
//     res.headers.as = 'raw';
//     res.sendNotOk(null, 'it failed');
// });

// server.listen(4041, '127.0.0.1', function onListen() {
//
// });

var clientChan = client.makeSubChannel({
    serviceName: 'server',
    peers: ['127.0.0.1:4040'],
    requestDefaults: {
        hasNoParent: true,
        headers: { 'as': 'raw', 'cn': 'example-client' }
    }
});

// function func1() {
//   return new Promise(function(resolve, reject) {
//     clientChan.request({
//         serviceName: 'server',
//         timeout: 1000
//     }).send('func1', 'arg 1', 'arg 2', function onResp(err, res, arg2, arg3) {
//         console.log('normal res:',{ arg2: arg2.toString(), arg3: arg3.toString()});
//     });
//   })
// }

clientChan.request({
    serviceName: 'server',
    timeout: 1000
}).send('func1', 'arg 1', 'arg 2', function onResp(err, res, arg2, arg3) {
    console.log('normal res:',{ arg2: arg2.toString(), arg3: arg3.toString()});
});

clientChan.request({
    serviceName: 'server'
}).send('func2', 'arg 1', 'arg 2', function onResp(err, res, arg2, arg3) {
    console.log('err res: ', { ok: res.ok, message: String(arg3) });
});
